//获得url链接中的参数
function getUrlKey(name) {
  return (
    decodeURIComponent(
      (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(location.href) || [
        ,
        ""
      ])[1].replace(/\+/g, "%20")
    ) || null
  );
}
//防抖函数
function debounce(func, delay) {
  let timer = null;
  return function(...args) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}
//获得焦点后选中
function focus(event) {
  event.currentTarget.select();
}

//刷新jwt的token刷新时间
function saveRefreshTime(param) {
  let nowTime = new Date();
  let lastRefreshTime = window.sessionStorage.getItem("refreshTime")
    ? new Date(window.sessionStorage.getItem("refreshTime"))
    : new Date(-1);
  let expireTime = new Date(Date.parse(window.sessionStorage.getItem("tokenExpire")));

  let refreshCount = 30; //滑动系数,定义用户最大的停止活跃时间(分钟)
  if (lastRefreshTime >= nowTime) {
    lastRefreshTime = nowTime > expireTime ? nowTime : expireTime;
    lastRefreshTime.setMinutes(lastRefreshTime.getMinutes() + refreshCount);
    window.sessionStorage.setItem("refreshTime", lastRefreshTime);
  } else {
    window.sessionStorage.setItem("refreshTime", new Date(-1));
  }
}

export default { getUrlKey, debounce, focus, saveRefreshTime };
